package com.jinmei.mail.oauth;

import org.apache.log4j.Logger;

import com.jinmei.mail.client.ExmailClientProxy;
import com.jinmei.mail.request.ExmailClientRequest;
import com.jinmei.mail.response.AccessTokenResponse;
import com.jinmei.remote.client.URLConnectionClient;
import com.jinmei.remote.response.JSONResponse;

/**
 * Get a new access token using OAuth protocol 
 * 
 * @author richard
 *
 */
public class OAuthTokenAccess {
	
	private static Logger logger = Logger.getLogger(OAuthTokenAccess.class);
	
	public static JSONResponse requestAccessToken() throws Exception {
		ExmailClientRequest request = (ExmailClientRequest) ExmailClientRequest
                .tokenLocation(OAuth.URI_GET_TOKEN)
                .setClientId(OAuth.CLIENT_ID)
                .setClientSecret(OAuth.CLIENT_SECRET)
                .setGrantType(OAuth.CLIENT_CREDENTIALS)
                .buildQueryMessage();
		
		ExmailClientProxy client = new ExmailClientProxy(new URLConnectionClient());
		
        JSONResponse jsonResponse = null;
        Class<? extends JSONResponse> cl = AccessTokenResponse.class;
        jsonResponse = client.connectServer(request, cl);

        return jsonResponse;
	}

}
